﻿using Net5ApiAndEfMySql.DtoModel.Common;
using Net5ApiAndEfMySql.DtoModel.SystemAdmin;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;
using Net5CommonLib.SystemLib.TypeTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Net5ApiAndEfMySql.Repository.SystemAdmin
{
    public class AdminUserRepository : BaseRepository, IAdminUserRepository
    {
        private IRoleDbRepository _iRoleDbRepository;
        //public IRoleDbRepository MyProperty { get {
        //        return _iRoleDbRepository;
        //    } set {
        //        _iRoleDbRepository = value;
        //    } }
        private SoaSystemCenterDbContext _soaSystemCenterDbContext;
        public AdminUserRepository(IRoleDbRepository iRoleDbRepository,
            SoaSystemCenterDbContext soaSystemCenterDbContext)
        {
            _iRoleDbRepository = iRoleDbRepository;
            _soaSystemCenterDbContext = soaSystemCenterDbContext;
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AdminUserAdd(AdminUserDto user)
        {
            if (user == null) return false;
            //起一个事务
            using (var tran = _soaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    //先添加用户
                    _soaSystemCenterDbContext.Add(user);
                    //循环添加角色
                    if (user.RoleList != null && user.RoleList.Count > 0)
                    {
                        foreach (var r in user.RoleList)
                        {
                            AdminUserRoleRelationPo arrp = new AdminUserRoleRelationPo()
                            {
                                user_name = user.user_name,
                                role_id = r.role_id,
                                creator_date = DateTime.Now,
                                creator_name = user.creator_name
                            };
                            _soaSystemCenterDbContext.Add(arrp);
                        }
                    }
                    //保存用户和用户角色关系的添加
                    _soaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();

                }

            }
            return true;

        }

        public bool AdminUserUpdate(AdminUserDto user)
        {
            if (user == null) return false;
            using (var tran = _soaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    var dbUser = _soaSystemCenterDbContext.AdminUserPo.FirstOrDefault(c => c.user_name == user.user_name);
                    ModelCopier.CopyModel(user, dbUser, "creator_name", "creator_date", "user_name", "pass_word");
                    //第一个Sql参数是p0,第二个是p1  依次往下 
                    string delSql = "DELETE FROM admin_user_role_relation WHERE user_name=@p0";
                    object p = _soaSystemCenterDbContext.Database.ExecuteSqlRaw(delSql, dbUser.user_name);
                    if (user.RoleList != null && user.RoleList.Count > 0)
                    {
                        foreach (var r in user.RoleList)
                        {
                            AdminUserRoleRelationPo arrp = new AdminUserRoleRelationPo()
                            {
                                user_name = user.user_name,
                                role_id = r.role_id,
                                creator_date = DateTime.Now,
                                creator_name = user.creator_name
                            };
                            _soaSystemCenterDbContext.Add(arrp);
                        }
                    }
                    _soaSystemCenterDbContext.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();

                }

            }
            return true;
        }

        public AdminUserPo GetAdminUserByUserName(string userName)
        {
            return _soaSystemCenterDbContext.AdminUserPo.FirstOrDefault(c => c.user_name == userName);
        }

        public AdminUserDto GetAdminUserDtoByUserName(string userName)
        {
            AdminUserPo userPo = _soaSystemCenterDbContext.AdminUserPo.FirstOrDefault(c => c.user_name == userName);
            AdminUserDto userDto = new AdminUserDto();
            ModelCopier.CopyModel(userPo, userDto);
            userDto.RoleList = _iRoleDbRepository.GetRoleListByUserName(userName);
            return userDto;
        }

        public PagerResult<AdminUserPo> GetPageList(SearchModel m)
        {
            IQueryable<AdminUserPo> iq = _soaSystemCenterDbContext.AdminUserPo.AsQueryable();
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;
                string userName = "user_name";
                if (tempDi.Keys.Contains(userName) && !string.IsNullOrEmpty(tempDi[userName]))
                {
                    iq = iq.Where(c => c.user_name.Contains(tempDi[userName]));
                }
                string userFullName = "user_full_name";
                if (tempDi.Keys.Contains(userFullName) && !string.IsNullOrEmpty(tempDi[userFullName]))
                {
                    iq = iq.Where(c => c.user_full_name.Contains(tempDi[userFullName]));
                }
                string userStatus = "user_status";
                if (tempDi.Keys.Contains(userStatus) && !string.IsNullOrEmpty(tempDi[userStatus]))
                {
                    iq = iq.Where(c => c.user_status == int.Parse(tempDi[userStatus].ToString()));
                }
            }
            return Pager(iq, m);
        }

        

  
    }
}
